<?php

class NewsletterTypeController extends AController {

    public function loadModel($id) {
        $model = DNewsletterType::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionCreate($parentId) {
        $this->layout = '//layouts/admin/main_none';
        $model = new DNewsletterType;

        if (isset($_POST['DNewsletterType'])) {
            $model->attributes = $_POST['DNewsletterType'];
            if ($model->save()) {
                Yii::app()->clientScript->registerScript('close-dialog', 'parent.$("#dialogForm").dialog("close");'
                );
            }
        }

        if (empty($model->parent_id))
            $model->parent_id = $parentId;

        $this->render('form', array(
          'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $this->layout = '//layouts/admin/main_none';
        $model = $this->loadModel($id);

        if (isset($_POST['DNewsletterType'])) {
            $model->attributes = $_POST['DNewsletterType'];
            if ($model->save()) {
                Yii::app()->clientScript->registerScript('close-dialog', 'parent.$("#dialogForm").dialog("close");'
                );
            }
        }

        $this->render('form', array(
          'model' => $model,
        ));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isAjaxRequest) {
            $result = array(
                'success' => false,
                'message' => '',
            );
            
            if ( ! in_array($id, Yii::app()->params['id_reserved']['newsletter_type']) ) {
                $model = $this->loadModel($id);
                if ($model->canDelete($id)) {
                    $model->delete();
                    $result['success'] = true;
                    $result['message'] = Yii::t('DadminModule.msg', 'deleted');
                } else {
                    $result['message'] = Yii::t('DadminModule.msg', 'menu_deny_delete');
                }
            } else {
                $result['message'] = Yii::t('DadminModule.msg', 'newsletter_type_deny_delete');
            }
            

            echo json_encode($result);
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {
        $model = DNewsletterType::model();

        $this->render('index', array(
          'model' => $model,
        ));
    }

    public function actionMenuShowFull() {
        $model = DNewsletterType::model();

        $this->renderPartial('_menu_full', array(
          'model' => $model,
        ));
    }

    /**
     * Show childs of Parent menu - AJAX
     * @param integer $parentId
     */
    public function actionMenuShowChild($parentId) {
        $menu = DNewsletterType::model()->getMenuChild($parentId);
        if (Yii::app()->request->isAjaxRequest) {
            $this->renderPartial('_menu_child', array(
              'menu'     => $menu,
              'parentId' => $parentId,
            ));
        }
        else
            throw new CHttpException(403); //forbidden
    }

    /**
     * Update sort order - AJAX
     */
    public function actionMenuUpdateOrder() {
        if ((Yii::app()->request->isAjaxRequest) && (isset($_POST['orderid']))) {
            $model     = DNewsletterType::model();
            $orderList = $_POST['orderid'];

            foreach ($orderList as $order => $typeId) {
                $record = $model->findByPk($typeId);
                $record->sort_order = $order;
                $record->save();
            }
        }
        else
            throw new CHttpException(403); //forbidden
    }

    /**
     * AJAX SUPPROTING FOR ASSIGNING LINK
     */
    public function actionAssignLink() {
        $this->layout = '//layouts/admin/main_none';
        $model = new DNewsletterType();

        $this->render('_assign_link', array(
          'model' => $model,
        ));
    }

}